var express = require('express');
var router = express.Router();
var retUtil = require('../../../../utils/retUtil');
var api4back = require('../../../../service/api4back');
var CONST = require('../../../../utils/const');
var crypto = require('crypto');

/**
 * 后台账号密码登录
 * @author 李强
 * @date 2018-07-10
 *
 * 参数：account : 用户账号
 * 参数：password : 用户密码(经过MD5加密)
 */
function middleStageLogin(req, res, next) {
    //获取参数
    var account = retUtil.stringFromReq(req, 'userName') || "";
    var password = retUtil.stringFromReq(req, 'password') || "";
    //参数判断
    if(!account || account.length == 0 || !password || password.length == 0){
        return retUtil.ret_err_json("请输入账号密码",res);
    }
    password = crypto.createHash('md5').update(password,'utf8').digest('hex');
    //数据请求
    var para = {
        body :{
            account:account,
            password:password,
        },
        method : "POST"
    };
    api4back.middleStageLogin(para,function (postErr, apiRes) {
        if(postErr) {
            return retUtil.ret_err_json(postErr, res);
        }
        req.session[CONST.SESSION_USER] = apiRes;
        return retUtil.ret_succ_json(apiRes,res);
    });
}
router.get('/middleStageLogin', middleStageLogin);
router.post('/middleStageLogin', middleStageLogin);

module.exports = router;
